// WS2812B_RMT.cpp
#include "ws2812b.h"
#include <Arduino.h>
#include "global_config.h"

extern WS2812B_RMT leds;

WS2812B_RMT::WS2812B_RMT(uint16_t n, uint8_t pin, rmt_channel_t rmtChannel) 
    : numLeds(n), channel(rmtChannel), brightness(255) {
    
    pixels = new uint8_t[numLeds * 3];
    clear();
    
    // 配置RMT
    rmt_config_t config = RMT_DEFAULT_CONFIG_TX((gpio_num_t)pin, channel);
    config.clk_div = 2; // 80MHz / 2 = 40MHz
    
    rmt_config(&config);
    rmt_driver_install(channel, 0, 0);
}

WS2812B_RMT::~WS2812B_RMT() {
    rmt_driver_uninstall(channel);
    delete[] pixels;
}

void WS2812B_RMT::setPixelColor(uint16_t n, uint8_t r, uint8_t g, uint8_t b) {
    if (n < numLeds) {
        uint16_t index = n * 3;
        pixels[index] = (g * brightness) >> 8;     // G
        pixels[index + 1] = (r * brightness) >> 8; // R  
        pixels[index + 2] = (b * brightness) >> 8; // B
    }
}

void WS2812B_RMT::setPixelColor(uint16_t n, uint32_t color) {
    setPixelColor(n, (color >> 16) & 0xFF, (color >> 8) & 0xFF, color & 0xFF);
}

void WS2812B_RMT::setBrightness(uint8_t b) {
    brightness = b;
}

void WS2812B_RMT::show() {
    // 创建RMT项目数组
    rmt_item32_t *items = new rmt_item32_t[numLeds * 24];
    int itemIndex = 0;
    
    for (int i = 0; i < numLeds * 3; i++) {
        for (int bit = 7; bit >= 0; bit--) {
            if (pixels[i] & (1 << bit)) {
                // 位 '1': T0H=0.7us, T0L=0.6us
                items[itemIndex].level0 = 1;
                items[itemIndex].duration0 = 28; // 0.7us * 40MHz = 28
                items[itemIndex].level1 = 0;
                items[itemIndex].duration1 = 24; // 0.6us * 40MHz = 24
            } else {
                // 位 '0': T0H=0.35us, T0L=0.8us
                items[itemIndex].level0 = 1;
                items[itemIndex].duration0 = 14; // 0.35us * 40MHz = 14
                items[itemIndex].level1 = 0;
                items[itemIndex].duration1 = 32; // 0.8us * 40MHz = 32
            }
            itemIndex++;
        }
    }
    
    // 发送数据
    rmt_write_items(channel, items, numLeds * 24, true);
    delete[] items;
}

void WS2812B_RMT::clear() {
    for (uint16_t i = 0; i < numLeds * 3; i++) {
        pixels[i] = 0;
    }
}

uint16_t WS2812B_RMT::numPixels() const {
    return numLeds;
}

uint32_t WS2812B_RMT::Color(uint8_t r, uint8_t g, uint8_t b) {
    return ((uint32_t)r << 16) | ((uint32_t)g << 8) | b;
}


void rainbow_ws2812b_test() {
    // 彩虹效果
    for (int i = 0; i < NUM_LEDS; i++) {
        int hue = (i * 256 / NUM_LEDS) + (millis() / 10);
        hue %= 256;
        
        uint8_t r, g, b;
        if (hue < 85) {
            r = hue * 3;
            g = 255 - hue * 3;
            b = 0;
        } else if (hue < 170) {
            hue -= 85;
            r = 255 - hue * 3;
            g = 0;
            b = hue * 3;
        } else {
            hue -= 170;
            r = 0;
            g = hue * 3;
            b = 255 - hue * 3;
        }
        
        leds.setPixelColor(i, r, g, b);
    }
    leds.show();
    delay(20);
}

// 设置为亮蓝色
void WS2812B_RMT::setBrightBlue() {
    for (uint16_t i = 0; i < numLeds; i++) {
        // 亮蓝色: R=0, G=0, B=255
        setPixelColor(i, 0, 0, 255);
    }
    show(); // 立即更新显示
}


// 设置为亮绿色
void WS2812B_RMT::setBrightGreen() {
    for (uint16_t i = 0; i < numLeds; i++) {
        // 亮蓝色: R=0, G=0, B=255
        setPixelColor(i, 0, 255, 0);
    }
    show(); // 立即更新显示
}


// 全部熄灭
void WS2812B_RMT::turnOff() {
    clear();
    show(); // 立即更新显示
}


void WS2812B_Toggle()
{
    static bool toggle = 0;
    if(toggle)
    {
        toggle = 0;
        leds.setBrightBlue();
    }
    else
    {
        toggle = 1; 
        leds.turnOff();
    }
}